package com.ptu.server.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.ptu.dto.SeckillProductDTO;
import com.ptu.entity.SeckillProduct;
import com.ptu.mapper.SeckillProductMapper;
import com.ptu.server.SeckillProductServer;
import com.ptu.vo.SeckillProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SeckillProductServerImpl implements SeckillProductServer {

    @Autowired
    private SeckillProductMapper seckillProductMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Transactional
    @Override
    public void addSeckillProduct(SeckillProductDTO seckillProductDTO) {
        SeckillProduct seckillProduct = new SeckillProduct();

        BeanUtil.copyProperties(seckillProductDTO, seckillProduct);

        seckillProduct.setStatus(0);

        seckillProductMapper.addSeckillProduct(seckillProduct);

        //保存到Redis
        stringRedisTemplate.opsForValue().set("seckill:stock:" + seckillProduct.getName(), seckillProduct.getStock().toString());

    }

    @Override
    public List<SeckillProduct> list() {
        List<SeckillProduct> list = seckillProductMapper.list();
        return list;
    }

    @Transactional
    @Override
    public void delete(Long id) {
        SeckillProduct seckillProduct = seckillProductMapper.getDetailById(id);
        System.out.println("删除商品" + seckillProduct.getName());

        seckillProductMapper.deleteById(id);
        //删除redis缓存
        stringRedisTemplate.delete("seckill:stock:" + seckillProduct.getName());
//        stringRedisTemplate.opsForValue().set("seckill:stock:" + seckillProduct.getName(), seckillProduct.getStock().toString());
    }

    @Transactional
    @Override
    public void update(SeckillProductDTO seckillProductDTO) {
        //根据name查询数据库
        SeckillProduct seckillProduct = seckillProductMapper.getDetailById(seckillProductDTO.getId());
        if (seckillProduct == null) {
            return;
        }
        //删除缓存
        stringRedisTemplate.delete("seckill:stock:" + seckillProduct.getName());

        seckillProductMapper.update(seckillProductDTO);
        stringRedisTemplate.opsForValue().set("seckill:stock:" + seckillProductDTO.getName(), seckillProductDTO.getStock().toString());
    }

    @Override
    public SeckillProduct detail(Long id) {
        SeckillProduct detail = seckillProductMapper.getDetailById(id);
        return detail;
    }

    @Override
    public void status(Long id) {
        SeckillProduct detail = seckillProductMapper.getDetailById(id);
        if (detail.getStatus() == 1) {
            System.out.println("下架秒杀商品" + id);
            seckillProductMapper.updateStatusById(id, 0);
        } else {
            System.out.println("上架秒杀商品" + id);
            seckillProductMapper.updateStatusById(id, 1);
        }

    }

    @Override
    public List<SeckillProductVO> userGetList() {
        List<SeckillProductVO> list = seckillProductMapper.userGetList();
        return list;
    }
}
